package my.learnDB.mybatis.daoAnno;

import my.learnDB.mybatis.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 一个人有多个 存款的账户（支付宝，微信，其他银行等等）
 * User ---> Accounts
 * 二级缓存：注解开发，关系开发时：会调用其他dao里面的方法
 */
@CacheNamespace(blocking=true)
public interface UserDao {


    // findById
    @Select("select * from user where id = #{id}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),  // column: 数据库字段， property：实体类属性
            @Result(column = "username", property = "name"),
            @Result(column = "sex", property = "gender"),
            @Result(column = "password", property = "pwd")
    })
    User findById(Integer id);

    // ************关系*********************

    // 一对多
    @Select("select * from user")
    @Results(id = "userMap",  // 结果集id，便于复用
            value = {
                    @Result(id = true, column = "id", property = "id"),  // column: 数据库字段， property：实体类属性
                    @Result(column = "username", property = "name"),
                    @Result(column = "sex", property = "gender"),
                    @Result(column = "password", property = "pwd"),

                    // 大型翻车现场，注意方法的调用
                    @Result(column = "id", property = "accounts",
                            many = @Many(select = "my.learnDB.mybatis.daoAnno.AccountDao.findByUid", fetchType = FetchType.EAGER))
            })
    List<User> relationFindAll();
}
